import tensorflow._api.v2.compat.v1 as tf
import numpy as np
tf.disable_eager_execution()
np.random.seed(2)
x_data=np.linspace(-1,1,1000)
y_data=2*x_data+1.0+np.random.randn(*x_data.shape)*0.4
x=tf.placeholder(tf.float32)
y=tf.placeholder(tf.float32)
def model(x,w,b):
    return tf.multiply(w,x)+b
w=tf.Variable(1.0)
b=tf.Variable(0.0)
pred=model(x,w,b)
epochs=30
learn_rate=0.005
lossf=tf.reduce_mean(tf.pow(y-pred,2))
opt=tf.train.GradientDescentOptimizer(learn_rate).minimize(lossf)
sess=tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(1,epochs+1):
    null,loss=0.0,0.0
    for xs,ys in zip(x_data,y_data):
        null,loss=sess.run([opt,lossf],feed_dict={x:xs,y:ys})
    if (epoch % 10 == 0):
        print("loss:", loss, "epoch:", epoch)
print(sess.run(w))
print(sess.run(b))
x_test=3
y_text=2*x_test+1
print(sess.run(pred,feed_dict={x:x_test}),y_text,sep='||')
sess.close()